package com.yd.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.junit.Test;


public class PostSend
{
	
	@Test
	public void test(){
		String r="您好您的短信验证码是"+randomUtil.math(6)+"【3024宿舍专用】";
		
		String res=this.PostSendDuanxin("http://service.winic.org:8009/sys_port/gateway/index.asp", "a1049407088", "200600735a", "18678672716", 
				this.UTFtoGB(r),"");
	   System.out.println(res);
	   System.out.println(r);
	}
	
	
	
	
	public String PostSendDuanxin(String url,String id,String pwd,String to,String text,String time){
		String param="id="+id+"&pwd="+pwd+"&to="+to+"&content="+text+"&time="+time;
	    String result=this.sendPost(url, param);
		return result;
	}
	
	

	public String sendPost(String url, String param)
	{
		BufferedReader br = null;
		PrintWriter pw = null;
		StringBuffer sb = new StringBuffer();
		try
		{
			URL ur = new URL(url);
			URLConnection conn = ur.openConnection();
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			pw = new PrintWriter(conn.getOutputStream());
			pw.write(param);
			pw.flush();

			br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			String line = "";
			while ((line = br.readLine()) != null)
			{
				sb.append(line);
			}

		} catch (MalformedURLException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (br != null)
				{
					br.close();
				}
				if (pw != null)
				{
					pw.close();
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}

		}

		return sb.toString();
	}
	
	public String UTFtoGB(String aa){
    	String GB1="";
    	try
		{
			 GB1=new String(aa.getBytes("UTF-8"),"gb2312");
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
    	return GB1;
    	
    }
	

}
